package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.HeSuan;
import com.example.demo.entity.NocvData;
import com.example.demo.entity.NocvNews;
import com.example.demo.entity.Vaccine;
import com.example.demo.service.IndexService;
import com.example.demo.service.NocvNewsService;
import com.example.demo.vo.DataView;
import com.example.demo.vo.NocvNewsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import static com.example.demo.service.NocvNewsService.*;

/**
 * @author Coding路人王
 * @date 2022/6/10 8:31
 */
@Controller
@RequestMapping("/news")
public class NocvNewsController {
    @Autowired
    private NocvNewsService nocvNewsService;



    @RequestMapping("/toNews")
    public String toNews(){
        return "news/news";
    }


    @RequestMapping("/listNews")
    @ResponseBody
    public DataView listNews(NocvNewsVo nocvNewsVo) {
        // 1. 创建分页的对象 当前页 每页限制大小
        IPage<NocvNews> iPage = new Page<>(nocvNewsVo.getPage(),nocvNewsVo.getLimit());
        // 2. 创建模糊查询条件
        QueryWrapper<NocvNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(nocvNewsVo.getTitle()),"title",nocvNewsVo.getTitle());
        nocvNewsService.page(iPage,queryWrapper);
        return new DataView(iPage.getTotal(),iPage.getRecords());
    }



    /**
     * 新增或者修改
     */
    @RequestMapping("/addNocvNews")
    @ResponseBody
    public DataView addNocvNews(NocvNews nocvNews){
        nocvNews.setCreateTime(new Date());
        nocvNewsService.saveOrUpdate(nocvNews);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("添加新闻成功！");
        return dataView;
    }

    @RequestMapping("/UpdateNocvNews")
    @ResponseBody
    public DataView UpdateNocvNews(NocvNews nocvNews){
        DataView dataView = new DataView();
        nocvNews.setCreateTime(new Date());
        nocvNewsService.updateById(nocvNews);
        dataView.setCode(200);
        dataView.setMsg("修改新闻成功！");
        return dataView;
    }









    @RequestMapping("/deleteById")
    @ResponseBody
    public DataView deleteById(Integer id){
        nocvNewsService.removeById(id);
        DataView dataView = new DataView();
        dataView.setMsg("删除成功！");
        dataView.setCode(200);
        return dataView;
    }



}
